<?php
/**
 * Bootstrap类, 在这个类中, 所以以_init开头的方法
 * 都会被调用, 调用次序和申明次序相同
 * 
 * @author  Laruence
 * @date    2011-05-13 15:24
 * @version $Id$ 
*/

class Bootstrap extends Yaf_Bootstrap_Abstract {
	/**
	 * 把配置存到注册表
	 */
	function _initConfig(Yaf_Dispatcher $dispatcher) {
		$config = Yaf_Application::app()->getConfig();

		Yaf_Registry::set("config",  $config);
	}

	/**
	 * 执行CLI
	 */
	function _initRun($dispatcher) {
		$r = $dispatcher->getRequest();
		var_dump($r);
		exit;
		if(count($argv) < 2) {
			echo '参数错误';
			exit;
		}
		
		$options = array();
		foreach($argv as $v){
			if(preg_match('/^--([a-zA-Z]+)=([a-zA-Z1-9]+)/', $v, $m) AND count($m) == 3){
				$options[$m[1]] = $m[2];
			}
		}
		
		$fname = SCRIPTPATH. $argv[1].'.php';
		
		if(file_exists($fname)){
			require(SCRIPTPATH . 'AbstractCli.php');
			require_once($fname);
		}
		else{
			echo 'File not exists:\''.$fname.'\'';
			exit;
		}
		if( ! class_exists(ucfirst($argv[1]))){
			echo 'Class not exists:\''.$argv[1].'\'';
			exit;
		}
		
		try{
		
		    $class = new $argv[1]($options);
		
		    $class->Main();
		}
		catch(Exception $e){
		    echo $e->getMessage();
		}
	}
}
